<?php echo $this->element('displayHeader'); ?>
<div class="toolbar"><?php echo $this->element('toolbar', array('buttons' => array('search', 'refresh'))); ?></div>

<?php if (empty($this->data)) : // ถ้าไม่มีข้อมูล ?>
    <?php echo $this->element('dataNotFound'); ?>
<? else : // ถ้ามีข้อมูล ?>
    <?php //$paginator->options(array('update' => 'content', 'indicator' => 'indicator')); ?>
    <?php echo $this->element('dataCaptionLimit'); ?>
    <?php $cur = $paginator->counter(array('format' => '%start%')); ?>

    <table class="data">
        <thead>
            <tr>
                <th class="no"><?php echo $paginator->sort('No', 'id'); ?></th>
                <th><?php echo $paginator->sort('Request no.', 'DeRH.request_no'); ?></th>
                <th width="90px"><?php echo $paginator->sort('Request Date', 'DeRH.request_date'); ?></th>
                <th><?php echo $paginator->sort('Customer', 'Customer.name'); ?></th>
                <th><?php echo $paginator->sort('Site', 'Site.name'); ?></th>
                <th width="90px"><?php echo $paginator->sort('Request Delivery Date', 'DeRH.due_date'); ?></th>
                <th width="90px"><?php echo $paginator->sort('Actual Delivery Date', 'DeRH.supplier_due_date'); ?></th>
                <th><?php echo $paginator->sort('Customer Doc. No.', 'DeRH.custoemr_document_no'); ?></th>
                <th><?php echo $paginator->sort('Status', 'DeRStatus.title'); ?></th>
                <th><?php echo $paginator->sort('End User', 'DeRH.end_user'); ?></th>
                <th><?php echo $paginator->sort('Request by', 'DeRH.request_by'); ?></th>
                <th>Invoiced</th>
                <th class="action">&nbsp;</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($this->data as $i => $data): ?>
                <tr class="<?php echo $data['DeRH']['is_invoiced'] == true ? 'green' : ((($i + 1) % 2 == 0) ? 'even-cell' : 'odd-cell'); ?>">
                    <td class="center"><?php echo $i + 1; ?></td>
                    <td>
                        <?php
                        echo $data['DeRH']['request_no'];
                        if ($data['DeRH']['is_returned']) {
                            echo $html->link(' ' . ($data['DeRtH']['delivery_return_status_id'] == DeRtStatus::RETURNING ? $html->image('icon/return2.png') : $html->image('icon/return.png')), array('controller' => 'supplier_delivery_returns', 'action' => 'view', 'delivery_request_head_id:' . $data['DeRH']['id']), array('target' => '_blank'), null, false);
                        }
                        ?>
                    </td>
                    <td class="center"><?php echo $data['DeRH']['request_date']; ?></td>
                    <td><?php echo $data['Customer']['name']; ?></td>
                    <td><?php echo $data['Site']['name']; ?></td>
                    <td><?php echo $data['DeRH']['due_date']; ?></td>
                    <td><?php echo $data['DeRH']['supplier_due_date']; ?></td>
                    <td><?php echo $data['DeRH']['customer_document_no']; ?></td>
                    <td><?php echo $data['DeRStatus']['title'] . ($data['DeRH']['is_partial'] ? ' (partial)' : ''); ?></td>
                    <td><?php echo $data['DeRH']['end_user']; ?></td>
                    <td class="center"><?php echo $data['DeRH']['request_by']; ?></td>
                    <td class="center">
                        <span id="spanInvoiced<?php echo $i; ?>">
                            <?php echo $data['DeRH']['is_invoiced'] == true ? (!empty($data['DeRH']['invoice_no']) ? $data['DeRH']['invoice_no'] : 'Yes') : 'No'; ?>
                        </span>
                        <?php if ($data['DeRH']['delivery_request_status_id'] > DeRStatus::ON_DELIVERY && $data['DeRH']['is_invoiced'] != true) : ?>
                            <span id="spanInvoicedChecked<?php echo $i; ?>" style="display: none;">
                                <?php echo $form->checkbox('DeRH.is_invoiced', array('id' => 'DeRHIsInvoiced' . $i, 'checked' => $data['DeRH']['is_invoiced'])); ?> Yes/No
                                <?php echo $form->text('DeRH.invoice_no', array('id' => 'DeRHInvoiceNo' . $i, 'style' => 'display: ' . ($data['DeRH']['is_invoiced'] == true ? 'block;' : 'none;'), 'value' => $data['DeRH']['invoice_no'])); ?>
                                <?php echo $html->link('Save', array('controller' => 'supplier_delivery_requests', 'action' => 'set_invoiced', $data['DeRH']['id']), array('id' => 'linkInvoiced' . $i, 'class' => 'hide')); ?>
                                <button id="buttonInvoicedChecked<?php echo $i; ?>">Save</button>
                                <?php echo $html->link('Cancel', '#', array('id' => 'linkCancel' . $i)); ?>
                            </span>
                            <script type="text/javascript">
                                $j(function() {
                                    var i = <?php echo $i; ?>;
                                    var spanChecked = "#spanInvoicedChecked" + i;
                                    var spanInvoiced = "#spanInvoiced" + i;
                                    var cancel = "#linkCancel" + i;
                                    var linkInvoiced = "#linkInvoiced" + i;
                                    var button = "#buttonInvoicedChecked" + i;
                                    var checkbox = "#DeRHIsInvoiced" + i;
                                    var invoiceNo = "#DeRHInvoiceNo" + i;

                                    $j(spanInvoiced).handover({highlight: 'td'}).bind('click', function(){
                                        $j(this).hide();
                                        $j(spanChecked).slideDown();
                                    });

                                    $j(cancel).bind('click', function() {
                                        $j(spanChecked).slideUp();
                                        $j(spanInvoiced).show();
                                    });

                                    $j(checkbox).bind('click', function() {
                                        if ($j(this).attr('checked')) {
                                            $j(invoiceNo).slideDown();
                                        }  else {
                                            $j(invoiceNo).slideUp();
                                        }
                                    });

                                    // กด save ถ้าติ๊ก invoice แสดงแถวเป็นสีเขียว ถ้า no ไม่มีอะไร
                                    $j(button).bind('click', function() {
                                        $j(spanInvoiced).update($j(linkInvoiced).attr('href') + '/' + $j(checkbox).is(':checked') + '/' + escape($j(invoiceNo).val()), function(data) {
                                            if (data == 'Yes') {
                                                $j(spanInvoiced).parents('tr').addClass('green');
                                                var inv = $j(invoiceNo).val();
                                                if (inv.length > 0) {
                                                    $j(spanInvoiced).text(inv);
                                                }
                                            } else {
                                                $j(spanInvoiced).parents('tr').removeClass('green');
                                            }
                                        });
                                        $j(cancel).click();
                                    });
                                });
                            </script>
                        <?php endif; ?>
                    </td>
                    <td><?php echo $html->link($html->image('icon/edit.png', array('title' => 'edit', 'alt' => 'edit')), '/supplier_delivery_requests/view/' . $data['DeRH']['id'], null, null, false); ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <?php echo $this->element('paging'); ?>
<? endif; ?>
<?php
//debug($this->data); ?>